// pages/f_put_question/index.js
var app = getApp();
const Toptips = require('../../dist/toptips/index');
var CommonUtils = app.CommonUtils;
var Urls = require('../../assets/data/f_urls');

Page(Object.assign({}, Toptips, {

  /**
   * 页面的初始数据
   */
	data: {
		inputMaxLength: 300, //input最大输入长度
		input1PlaceholderValue: '详细描述问题的内容，将有利于获得全面有效地解答。48小时内无人回答，将自动撤销问题。',
		input2PlaceholderValue: '进行有效的问题补充，将有利于避免获得无效的答案。',

		input1Value: '',
		input2Value: '',

		inputTotalValueLength: 0,
		input1MaxLength: 0,    //input最大输入长度
		input2MaxLength: 0,		//input2最大输入长度
		imageList: [],
		//uploadImageListIndex: 0, //问题图片排序

		iconList: ['../../images/22.png', '../../images/11.png'],
		radioBtn1IsSelect: false,
		radioBtn2IsSelect: false,

		cityList: [],
		isAnonymity: "0",
		istop: "0",
		topMoney: "0",
		tm_id: "0",
		Q_ID: "",
		myDialog: false,
    
	},

  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		console.log("aaaaa");
		console.log(options);
		var _this = this;
		//console.log(options.id);
		_this.setData({
			Q_ID: options.id,
      tm_id: options.id,
		})

		var tempInputMaxLength = this.data.inputMaxLength;
		this.setData({
			input1MaxLength: tempInputMaxLength,
			input2MaxLength: tempInputMaxLength,
		}),


			wx.request({
				url: "https://www.chnode.xyz/api/Question_Release.ashx?",
				data: {
					DataType: "Top_List",
				},
				success: function (res) {
					//console.log(res)
					if (res.data.isok == "false")
						_this.showZanTopTips(res.data.info);
					else {
						_this.setData({
							cityList: res.data.data
						})
						//console.log(_this.data.cityList)
					}
				}
			})


	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},

  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {

	},

  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {

	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {

	},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function () {

	},

  /**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom: function () {

	},

  /**
   * 用户点击右上角分享
   */
	onShareAppMessage: function () {

	},

	/**
	 * 输入框1的监听
	 */
	inputEvent1: function (e) {
		var _this = this;
		this.setData({
			input1Value: e.detail.value,
		})
		//console.log(_this.data.input1Value);
		this.showRestTextNumber();
	},
	/**
	 * 输入框2的监听
	 */
	inputEvent2: function (e) {
		var _this = this;
		this.setData({
			input2Value: e.detail.value,
		})
		//console.log(_this.data.input2Value);
		this.showRestTextNumber();
	},

	/**
	 * 控制两个输入框输入字数,显示剩余字数
	 */
	showRestTextNumber: function () {
		var input1ValueLength = this.data.input1Value.length;
		var input2ValueLength = this.data.input2Value.length;
		var inputTotalLength = input1ValueLength + input2ValueLength;
		if (inputTotalLength == this.data.inputMaxLength) {
			this.setData({
				input1MaxLength: input1ValueLength,
				input2MaxLength: input2ValueLength,
			})
		} else {
			var tempInputMaxLength = this.data.inputMaxLength;
			this.setData({
				input1MaxLength: tempInputMaxLength,
				input2MaxLength: tempInputMaxLength,
			})
		}
		//console.log(inputTotalLength)
		this.setData({
			inputTotalValueLength: inputTotalLength,
		})
	},


	/**
	 * 点击选择图片按钮事件
	 */
	btnEventSelectImage: function () {
		var _this = this;
		wx.showActionSheet({
			itemList: ['相册', '拍照'],
			itemColor: "#F75F47",
			success: function (res) {
				if (!res.cancel) {
					if (res.tapIndex == 0) {
						_this.chooseWxImage('album')
					} else if (res.tapIndex == 1) {
						_this.chooseWxImage('camera')
					}
				}
			}
		})
	},

	/**
	 * 选择图片
	 */
	chooseWxImage: function (type) {
		var _this = this;
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'],
			sourceType: [type],
			success: function (res) {
				var imgsArr = _this.data.imageList;
				imgsArr.push(res.tempFilePaths[0]);
				_this.setData({
					imageList: imgsArr
				})
			}
		})
	},

	/**
	 * Radio1Btn 点击事件
	 */
	btnEventRadio1Btn: function () {
		var _this = this
		_this.setData({
			radioBtn1IsSelect: !_this.data.radioBtn1IsSelect
		})
		if (!_this.data.radioBtn1IsSelect) {
			//console.log("meidianji");
			_this.setData({
				isAnonymity: "0"
			})
		} else {
			//console.log("dianji");
			_this.setData({
				isAnonymity: "1"
			})
		}
	},
	/**
	 * Radio2Btn 点击事件
	 */
	btnEventRadio2Btn: function () {

		var _this = this
		//console.log(_this.data.cityList);

		var i = 0;
		var tempList = [];
		var str_all;
		for (var i = 0; i < _this.data.cityList.length; i++) {
			//console.log("11111");
			//console.log(i);
			str_all = "¥" + _this.data.cityList[i].TM_Money + "     " + _this.data.cityList[i].TM_Name;
			tempList[i] = str_all;
		}

		_this.setData({
			radioBtn2IsSelect: !_this.data.radioBtn2IsSelect
		})
		if (!_this.data.radioBtn2IsSelect) {
			//console.log("11111");
			_this.setData({
				istop: "0"
			})
		} else {
			//console.log("2222222");
			// _this.bindCityPickerChange();
			//////////

			wx.showActionSheet({
				itemList: tempList,
				success: function (res) {
					// console.log("wwwwww");
					// console.log(_this.data.cityList[res.tapIndex].TM_Money);
					// console.log(_this.data.cityList[res.tapIndex].TM_ID);
					// console.log("bbbbbb");
					// console.log(res.tapIndex);
					_this.setData({
						istop: "1",
						topMoney: _this.data.cityList[res.tapIndex].TM_Money,
						tm_id: _this.data.cityList[res.tapIndex].TM_ID,
					})

				},
				fail: function (res) {
					_this.setData({
						radioBtn2IsSelect: !_this.data.radioBtn2IsSelect,
						istop: "0"
					})
				}
			})
		}
	},

	bindCityPickerChange: function (e) {
		var index = e.detail.value;
		this.setData({
			cityListIndex: index,
		})
	},
	/**
	 * 支付并发布点击事件
	 */
	btnEventBuyAndPut: function () {

		var _this = this;

		var _city;
		var _Token;
		try {
			var value = wx.getStorageSync('city')
			if (value) {

				_city = value;
				// Do something with return value
			}
		} catch (e) {
			// Do something when catch error
		}

		try {
			var value = wx.getStorageSync('Token')
			if (value) {

				_Token = value;
				// Do something with return value
			}
		} catch (e) {
			// Do something when catch error
		}

		console.log(_Token);
		console.log(_this.data.Q_ID);
		console.log(_city);
		console.log(_this.data.topMoney);
		console.log(_this.data.tm_id);
		console.log(_this.data.input1Value);
		console.log(_this.data.input2Value);

		

  

		//_this.uploadImages(24, _this.data.imageList, 1)

		wx.request({
			url: "https://www.chnode.xyz/api/Question_Release.ashx?",
			data: {
				DataType: "Release",
				token: _Token,
				note: _this.data.input1Value,
				supplement: _this.data.input2Value,
				isAnonymity: _this.data.isAnonymity,
				istop: _this.data.istop,
				topMoney: _this.data.topMoney,
				tm_id: _this.data.tm_id,
				qtid: _this.data.Q_ID,
				cid: _city,

			},
			success: function (res) {
				console.log(res)
				if (res.data.isok == "false")
					_this.showZanTopTips(res.data.info);

				else {

					console.log("发布成功");
_this.setData({
  Q_ID: res.data.data.Q_ID,
})
          _this.uploadImages(_this.data.Q_ID, _this.data.imageList, 1)
          console.log("dddddddddvvvvvvvvvvv");
          console.log(_this.data.Q_ID);
					if (_this.data.topMoney == 0) {

						wx.showModal({
							title: '提示',
							content: '发布成功',
							success: function (res) {
								if (res.confirm) {
									console.log('用户点击确定');
									wx.navigateBack({
										delta: 1
									})
								} else if (res.cancel) {
									console.log('用户点击取消')
								}
							}
						})


					} else {
						try {
							var value = wx.getStorageSync('openid')
							if (value) {
								// Do something with return value

								var openid = value;
								console.log(value);
								console.log(res.data.data.Q_ID);

								wx.request({
									url: "https://www.chnode.xyz/api/Pay.ashx?",
									data: {
										DataType: "TMoney_WxPay",
										openid: openid,
										Token: _Token,
										Q_ID: res.data.data.Q_ID

									},
									success: function (res1) {
										console.log(res1)
										if (res1.data.isok == "false")
											_this.showZanTopTips(res1.data.info);
										else {

											wx.requestPayment({
												timeStamp: res1.data.data.timeStamp,
												nonceStr: res1.data.data.nonceStr,
												"package": res1.data.data.package,
												signType: 'MD5',
												paySign: res1.data.data.paySign,
												fail: function (aaa) {
													wx.showToast({ title: '支付失败:' + aaa })
												},
												success: function (e) {
													console.log(e);
													wx.showToast({ title: '支付成功' })
													wx.redirectTo({
														// url: '../order-list/index'
													});
												}
											})


										}
									}
								})

							}
						} catch (e) {
							// Do something when catch error
						}
					}


					//console.log(_this.data.cityList)
				}
			}
		})








	},

	shuomingClicked: function (e) {
		console.log("shuoming");
		this.setData({
			myDialog: true,
		})
	},

	myDialogCancel: function (e) {
		console.log("shuoming");
		this.setData({
			myDialog: false,
		})
	},

	/**
	 * 上传图片
	 */
	uploadImages: function (temp_id, imageList, index) {
		var _this = this;
		if (index >= imageList.length + 1)
			return;
		wx.showLoading()
		var temp_url = 'https://www.chnode.xyz/api/Question_Release.ashx?DataType=QImageUpload&Q_ID=' + temp_id + '&QI_Order=' + index + '&file=';

		console.log(temp_url)
    console.log(imageList[index-1])
		console.log(index)
		wx.uploadFile({
			url: temp_url,
			filePath: imageList[index - 1],
			name: 'file',
			success: function (res) {
				console.log('success')
				console.log(index)
				console.log(res.data)
			},
			fail: function (res) {
				console.log('fail')
				console.log(index)
				console.log(res.data)
			},
			complete: function (res) {
				if (index == imageList.length)
					wx.hideLoading();
				else {
					index = index + 1;
					_this.uploadImages(temp_id, imageList, index)
				}
			},
		})
	}
}))